package com.wangxiang.shop.member.controller;

import com.alibaba.fastjson.JSONObject;
import com.wangxiang.common.utils.R;
import com.wangxiang.shop.member.entity.MemberEntity;
import com.wangxiang.shop.member.service.LoginService;
import com.wangxiang.shop.member.service.MemberService;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;

@RestController
@RequestMapping("member/login")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private MemberService memberService;

    @RequestMapping("wx")
    public R wxLogin(@RequestBody HashMap<String,String> map) throws IOException {
        //微信小程序登录接口，需要填写自己的appid,secret(密钥),登录人前端同意后返回的授权码code
        //由于微信小程序授权方式改变，现在只能获取到用户和头像
        String wxUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=wxdee4f05bbdc7b5a0&secret=7f49620266498d253a62d5f0a10e0cbd&js_code="+map.get("code").toString()+"&grant_type=authorization_code";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(wxUrl);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        String html = EntityUtils.toString(response.getEntity(),"utf8");
        //实例实体
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setName(map.get("userName"));
        memberEntity.setImage(map.get("image"));
        //拿到openId，

        JSONObject jsonObject = JSONObject.parseObject(html);
        memberEntity.setOpenId(jsonObject.getString("openid"));

        MemberEntity wxlogin = loginService.wxlogin(memberEntity);
//        System.out.println("登录信息"+wxlogin);
        if (wxlogin.getStatus()!=0){
//            System.out.println("违规");
            return R.error("该用户"+wxlogin.getInvalidRemark());
        }
        return R.ok().put("info",wxlogin);
    }

    /**
     * 用户名密码登录
     * @param map
     * @return
     */
    @RequestMapping("user")
    public R loginByUser(@RequestBody HashMap<String,Object> map){
        String nullStr = "";
        if (nullStr.equals(map.get("loginName"))|nullStr.equals(map.get("password"))|map.get("password")==null|map.get("loginName")==null) {
            return R.error("用户名或密码不能为空");
        }
//        System.out.println(map);

        MemberEntity member = memberService.selectUserByLoginName(map.get("loginName").toString());
//        System.out.println(member);
        if (member==null){
            return R.error("用户不存在");
        }
        if (!map.get("password").equals(member.getPassword())){
            return R.error("密码错误");
        }
        if (member.getStatus()!=0){
            return R.error("该用户"+member.getInvalidRemark());
        }


        return R.ok().put("info",member);
    }

    //退出登录
    @RequestMapping("out")
    public R loginOut(@RequestBody Integer userId){
        memberService.line(userId,0);
        return R.ok();
    }

}
